//
//  _2FindPasswordViewController.swift
//  fsapp
//
//  Created by zguang on 17/8/3.
//  Copyright © 2017年 huangshengjie. All rights reserved.
//
    
    import UIKit
    import Alamofire
    import SVProgressHUD
class _2FindPasswordViewController: UIViewController {
        
        let registView = loginView(type:"regist2")
        var code  = String()
        var phoneNumber = String()
        init(_ code:String,_ phoneNumber:String) {
            super.init(nibName: nil, bundle: nil)
            self.code = code
            self.phoneNumber = phoneNumber
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            self.navigationItem.title = "找回密码"

            self.view.addSubview(registView.BackView)
            self.registView.accountLabel.removeFromSuperview()
            registView.accountLabel.removeFromSuperview()
            registView.upImageButton.removeFromSuperview()
            registView.headImageView.removeFromSuperview()
            registView.accountText.removeFromSuperview()
            self.registView.upvisibleButton.addTarget(self, action: #selector(hidePassword1(_:)), for: .touchUpInside)
            self.registView.visibleButton.addTarget(self, action: #selector(hidePassword2(_:)), for: .touchUpInside)
            self.registView.loginNow.addTarget(self, action: #selector(complete), for: .touchUpInside)
            // Do any additional setup after loading the view.
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          //self.registView.accountText.resignFirstResponder()
        //self.registView.codeText.resignFirstResponder()
      //  self.registView.passwordText.resignFirstResponder()
        //self.registView.phoneText.resignFirstResponder()
        self.registView.userText.resignFirstResponder()
        self.registView.confirmPasswordText.resignFirstResponder()
    }
    
        func hidePassword1(_ btn:UIButton){
            btn.isSelected = !btn.isSelected
            if btn.isSelected{
                self.registView.userText.isSecureTextEntry = false
            }else{
                self.registView.userText.isSecureTextEntry = true
            }
            
        }
        func hidePassword2(_ btn:UIButton){
            btn.isSelected = !btn.isSelected
            if btn.isSelected{
                self.registView.confirmPasswordText.isSecureTextEntry = false
            }else{
                self.registView.confirmPasswordText.isSecureTextEntry = true
            }
            
        }
        
        
        func complete(){
            if (self.registView.userText.text == registView.confirmPasswordText.text){
                Alarequest()
            }else{
                let alert = UIAlertView(title: "提示", message: "两次输入的密码不一致", delegate: nil, cancelButtonTitle: "知道了")
                alert.show()
            }
            
        }
        
        
        
        // MARK: - 网络请求
        func Alarequest(){
            let url = LOGIN_API_URL + "RetrievePassword"
            let password = self.registView.confirmPasswordText.text
            
            
            let paramdic =
                ["phoneNumber":phoneNumber,
                 "password":password!,
                 "code":Int(code) as Any
                 ]
            print(paramdic["password"])
            
            
            Alamofire.request( url, method: .post, parameters: paramdic).responseJSON{ response in
                print("result=========================")
                print(response.request!)  // original URL request
                print(response.response!) // HTTP URL response
                print(response.data!)     // server data
                print(response.result)   // result of response serialization
                
                if let JSON2 = response.result.value {
                    let js = JSON(JSON2)
                    print(js)
                    DispatchQueue.main.async {
                        if(js["status"]==200){
                            //bo = true
                            print(js)
                            let alert = UIAlertView(title: "提示", message: "修改成功", delegate: nil, cancelButtonTitle: "知道了")
                            alert.show()
                            self.navigationController?.popToRootViewController(animated: true)
                            //print(bo)
                        }
                            
                        else{
                            if js["status"] == 1005{
                                let alert = UIAlertView(title: "修改密码失败", message: "请重试", delegate: nil, cancelButtonTitle: "知道了")
                                alert.show()
                            }
                            if js["status"] == 1003{
                                let alert = UIAlertView(title: "验证码错误", message: "请重试", delegate: nil, cancelButtonTitle: "知道了")
                                alert.show()
                                self.navigationController?.popViewController(animated: true)
                            }
                            if js["status"] == 1004{
                                let alert = UIAlertView(title: "服务器出错", message: "请稍后重试", delegate: nil, cancelButtonTitle: "知道了")
                                alert.show()
                            }
                        }
                    }
                    
                }
            }
            
        }
        
        
        
        /*
         // MARK: - Navigation
         
         // In a storyboard-based application, you will often want to do a little preparation before navigation
         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
         }
         */
        
}
